package com.es.hr.manage;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.es.main.LoginUserBeen;
import com.es.util.common.CommonBiz;
import com.es.util.excel.EmpExcelOrderBean;
import com.es.util.excel.ExportExcelJsp;
import com.es.util.page.PageNavigator;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.ActionSupport;

public class ExceptExcelEmpListAction  extends ActionSupport implements ServletRequestAware, ServletResponseAware{

	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	 HttpServletResponse response;

	  @SuppressWarnings("unchecked")
	public String execute() throws Exception
	  {
		  request.setCharacterEncoding("GB2312");
		   String queryName = request.getParameter("queryName")==null?"":request.getParameter("queryName");
		   String shiyebu = request.getParameter("shiyebu")==null?"":request.getParameter("shiyebu");
		   String emp_role = request.getParameter("emp_role")==null?"":request.getParameter("emp_role");
		   String emp_city_code = request.getParameter("emp_city_code")==null?"":request.getParameter("emp_city_code");
		   String emp_province_code = request.getParameter("emp_province_code")==null?"":request.getParameter("emp_province_code");
		   String emp_area_code = request.getParameter("emp_area_code")==null?"":request.getParameter("emp_area_code");
		   String queryEmpCode = request.getParameter("queryEmpCode")==null?"":request.getParameter("queryEmpCode");
		   String queryEmpJob = request.getParameter("queryEmpJob")==null?"":request.getParameter("queryEmpJob");
		   String queryRtm="";
		   ManageBiz manegeBiz = new ManageBiz();		
		   	   
		   LoginUserBeen userBean = (LoginUserBeen)request.getSession(false).getAttribute("loginuser");
		   String role  = userBean.getLoginRole();	
		   //所有的查询都包含自己
		   String emp_id = userBean.getLoginId(); 
		   //当前角色能访问的权限组
		   String[] emp_role_ary = null; 
			if("zongbu1".indexOf(role)>=0){
				emp_role_ary = new String[]{"all","all","all","all","all"};//权限标识不存在，代表可以看见全部
			}else if("shiyebu".equals(role)){
				shiyebu = userBean.getShiyebu()==null?"":userBean.getShiyebu();
				emp_role_ary = new String[]{"1","zongbu","shiyebu","1","zongbu"};
			}else if("RTM".equals(role)){
				emp_area_code = userBean.getEara_code()==null?"":userBean.getEara_code();
				shiyebu = userBean.getShiyebu()==null?"":userBean.getShiyebu();
				emp_role_ary = new String[]{"1","zongbu","shiyebu","RTM","zongbu"};
				queryRtm = emp_id;
			}else {//CTM&&其他
				emp_area_code = userBean.getEara_code()==null?"":userBean.getEara_code();
				shiyebu = userBean.getShiyebu()==null?"":userBean.getShiyebu();
				emp_city_code = userBean.getCity_code()==null?"":userBean.getCity_code();
				emp_province_code = userBean.getProvince_code()==null?"":userBean.getProvince_code();
				emp_role_ary = new String[]{"1","zongbu","shiyebu","RTM","CTM"};
				queryEmpCode = userBean.getEmp_code()==null?"":userBean.getEmp_code();
			}			
			   
		   CommonBiz biz = new CommonBiz(userBean);
		   
		   //区域列表
		   request.setAttribute("areaList", biz.getAreaList());
		   //角色
		   request.setAttribute("roleList", biz.getCommonList("role"));
		   //省份列表
		   if(emp_area_code!=null)
			   request.setAttribute("provinceList", biz.getProvinceList(emp_area_code));
		   //城市列表
		   if(emp_province_code!=null)
			   request.setAttribute("cityList", biz.getCityList(emp_province_code));
		   
		   ArrayList<String> param = new ArrayList<String>();
		   param.add(emp_area_code);
		   param.add(emp_province_code);
		   param.add(emp_city_code);
		   param.add(shiyebu);
		   param.add(emp_role);
		   param.add(queryName);	
		   param.add(queryEmpCode);
		   param.add(queryEmpJob);
		   param.add(queryRtm);
		   param.add(emp_id);
		   param.add(emp_role_ary[0]);
		   param.add(emp_role_ary[1]);
		   param.add(emp_role_ary[2]);
		   param.add(emp_role_ary[3]);
		   param.add(emp_role_ary[4]);
		   ArrayList<HashMap<String,String>> list  = manegeBiz.exceptExcelEMpList(param);
		  String excelString = ExportExcelJsp.getExcelJspString(list,EmpExcelOrderBean.getEmpOrderMap());
		  
		  //String filename="测试";
		  excelString = "<html><head><meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel;charset=gb2312\" /><title></title></head><body > "+excelString+"</body></html>";
		  response.setContentType("application/vnd.ms-excel;charset=gb2312");
		  //response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gbk"),"iso8859-1")+".xls" );
		  response.getWriter().write(excelString);
	      return "";
	  }

	  public void setServletRequest(HttpServletRequest request) {
	    this.request = request;
	  }

	  public void setServletResponse(HttpServletResponse response) {
	    this.response = response;
	  }
	}
